package cm.com.carlamanda.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.carlamanda.dao.InputParamDAO;
import cm.com.carlamanda.dao.InterfaceDAO;
import cm.com.carlamanda.domain.Interface;
import cm.com.carlamanda.domain.Page;
import cm.com.carlamanda.dto.InterfaceDto;
import cm.com.carlamanda.service.InterfaceService;

@Service
public class InterfaceServiceImpl implements InterfaceService {
    
    @Autowired
    private InterfaceDAO interfaceDao;
    @Autowired
    private InputParamDAO inputParamDao;
    
    @Override
    public InterfaceDto get(String id) {
        InterfaceDto result = interfaceDao.getInterface(id);
        return result;
    }

    @Override
    public int edit(Interface inter) {
        return interfaceDao.updateInterface(inter);
    }

    @Override
    public int add(Interface inter) {
        if(StringUtils.isEmpty(inter.getId())){
            inter.setId(UUID.randomUUID().toString());
        }
        return interfaceDao.insertInterface(inter);
    }

    @Override
    public int del(String id) {
        return interfaceDao.deleteInterfaceById(id);
    }

    @Override
    public List<InterfaceDto> search(InterfaceDto inter,Page page) {
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("name", inter.getName());
        map.put("url", inter.getUrl());
        if(inter.getUser()!=null){
            map.put("user", inter.getUser());
        }
        map.put("startPos", page.getStartPos());
        map.put("endPos", page.getEndPos());
        map.put("sortColumn", page.getSortColumn());
        map.put("sortAsc", page.getSortAsc());
        int count =interfaceDao.searchInterfacesCount(map);
        page.setTotalCount(count);
        page.setTotalPage(count);
        return interfaceDao.searchInterfaces(map);
    }
    
    @Override
    public void linkModule(String moduleId,String interfaceId){
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("moduleId", moduleId);
        map.put("interfaceId", interfaceId);
        interfaceDao.updateModuleId(map);
    }

    @Override
    public void batchAddInterface(List<Interface> list) {
        interfaceDao.batchInsertInterface(list);
    }

    @Override
    public void batchAddParam(Interface inter) {
        interfaceDao.batchInsertParam(inter);
    }

}
